use std::sync::Arc;
use futures::stream::SplitSink;
use log::info;
use tokio::net::TcpStream;
use tokio::sync::Mutex;
use tokio_tungstenite::{MaybeTlsStream, WebSocketStream};
use tokio_tungstenite::tungstenite::Message;
use crate::feature::Base;
use crate::feature::comfort::Comfort;
use crate::feature::weather::Weather;
use crate::group::{send_ai_message, send_message_group, send_one_message};
use crate::model::WebSocketMessage;

pub async fn handle(session_id: Arc<Mutex<String>>, msg_string: String, arc_writer: Arc<Mutex<SplitSink<WebSocketStream<MaybeTlsStream<TcpStream>>, Message>>>) {
    if let Ok(msg) =  serde_json::from_str::<WebSocketMessage>(msg_string.as_str()) {
        println!("{:?}", msg);
        if let Some(d) = msg.d.clone() {
            if let Some(content) = d.content.clone() {
                let content = content.replace(" ", "");
                info!("接受内容：{}", content);
                if content.eq("/Ping") {
                    send_message_group(Some("/Pong".to_string()), msg.clone()).await;
                } else if content.starts_with("/聊天") {
                    send_ai_message(d.clone().content, msg.clone()).await;
                }else if content.starts_with("/一言") {
                    send_one_message(d.clone().content, msg.clone()).await;
                } else if content.starts_with("/当前天气") {
                    Weather::send_message(d.clone().content, msg.clone()).await;
                } else if content.starts_with("/安慰文案") {
                    Comfort::send_message(d.clone().content, msg.clone()).await;
                } else {
                    send_message_group(Some("你好啊！暂时无法识别您的内容！".to_string()), msg.clone()).await;
                }
            }
        }
    }
}